package com.metacube.metaconnect.model;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

import com.sun.mail.iap.Response;

public class FileUpdate extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private FormFile file;

	public FormFile getFile() {
		return file;
	}

	public void setFile(FormFile file) {
		this.file = file;
	}

	/*
	 * validates update file picture form
	 */
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		ActionErrors errorList = new ActionErrors();

		try {
			if (file.getFileSize() == 0) {
				errorList.add("file", new ActionMessage(
						"MetaConnect.error.file"));
			}

			else if (!file.getContentType().equals("image/jpeg")
					&& !file.getContentType().equals("image/png")
					&& !file.getContentType().equals("image/gif")) {
				errorList.add("file", new ActionMessage(
						"MetaConnect.error.file_type"));
				request.setAttribute("errorInUpdatePic", "Invalid File!!!");
			}
			/**
			 * If the file size is greater than 20kb.
			 */
			else if (file.getFileSize() > 20480 * 25) {
				errorList.add("file", new ActionMessage(
						"MetaConnect.error.file_size"));
				request.setAttribute("errorInUpdatePic", "Pic Size is large!!!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		request.setAttribute("updatePic", "yes");
		return errorList;
	}

}
